

/**
 * 创建store
 * @param {*} reducer 
 * @returns 
 */
function createStore(reducer,initState){
    let state = initState
    let listeners = [];
    function getState(){
        return state
    }
    function dispatch(action){
        state = reducer(state,action) //返回新的state
        listeners.forEach(l=>l())
        // return action
    }
    function subscribe(listener){
        listeners.push(listener) 
        // 取消订阅 
        return()=>{
            listeners = listeners.filter(l => l !== listener)
        } 
    }
    dispatch({type:'@@/REDUX_INIT'}) //初始化state
    return {
        getState,
        dispatch,
        subscribe
    }
}
export default createStore